﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lu.Blog.Domain.Model.DocumentType;
using Lu.Blog.Domain.Utils;
using Lu.Blog.Models;
using umbraco.interfaces;
using umbraco.MacroEngines;
using Umbraco.Web.Mvc;
using Vega.USiteBuilder;

namespace Lu.Blog.Controllers
{
    public class GridSurfaceController : SurfaceController
    {
        //
        // GET: /GridSurface/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult HomePage()
        {
            var retColl = new Dictionary<string, IEnumerable<SummaryItem>>();

            var categories = BlogContentHelper.GetAllNonEmptyCategories();

            foreach (var dynamicCategory in categories)
            {
                var category = ContentHelper.GetByNodeId<Category>(dynamicCategory.Id);
                string key = category.Label;
                var list = new List<SummaryItem>();
                IEnumerable<INode> children = category.Descendants().Where(o => o.NodeTypeAlias == "ContentPage");
                foreach (var dynamicContentPage in children)
                {
                    var contentPage = ContentHelper.GetByNodeId<ContentPage>(dynamicContentPage.Id);
                    var imageUrl = String.Empty;
                    if (!string.IsNullOrEmpty(contentPage.PreviewImage))
                    {
                        imageUrl = XmlNodePickerHelper.GetImagesFromPicker(contentPage.PreviewImage).First().NiceUrl;
                    }
                    list.Add(new SummaryItem
                    {
                        SummaryBody = contentPage.PreviewBody,
                        SummaryTitle = contentPage.PreviewTitle,
                        SummaryImageUrl = imageUrl,
                        Page = contentPage
                    });
                }
                retColl.Add(key, list);
            }

            var viewModel = new GridViewModel
            {
                Items = retColl
            };
            return PartialView("GridViewWithFilters", viewModel);
        }
    }
}